package org.itboys.mobile.controller.small;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.service.mongo.weixin.SundayWeiXinActivityService;
import org.itboys.mobile.service.mongo.weixin.SundayWeiXinCollectionService;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author：贾琦磊
 * @Date：2018/7/17
 * @Description：收藏接口
 */
@Controller
@RequestMapping("/sunday/small/collect")
@Api(value = "collect",description = "收藏")
public class SundaySmallCollectController extends BaseController{


    @Autowired
    private SundayWeiXinActivityService activityService;
    @Autowired
    private SundayWeiXinCollectionService collectionService;


    @ApiOperation(value = "【小程序】用户收藏/取消收藏")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public void collection(HttpServletRequest request, HttpServletResponse response
            , @ApiParam(value = "用户id",required = true) @RequestParam("memberId")Long memberId
            , @ApiParam(value = "收藏对象id",required = true)@RequestParam("otherId")Long otherId
            , @ApiParam(value = "类型1--楼盘  2--活动",required = true)@RequestParam("type")Integer type
    ){
        try {
            ResponseMessage.success(collectionService.save(memberId,otherId,type));
        } catch (Exception e) {
            String msg = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/collect/activity----e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
    }

    /**
     * 获取我的关注列表
     * @param request
     * @param response
     */
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ApiOperation("【小程序】我的关注接口, 传入用户id")
    public void select(HttpServletRequest request,HttpServletResponse response,
                       @ApiParam(value = "用户id",required = true)@RequestParam("memberId")Long memberId,
                       @ApiParam(value = "起始页",required = true) @RequestParam(value = "pageNumber",required = true) String pageNumber,
                       @ApiParam(value = "每页大小",required = true) @RequestParam(value = "pageSize",required = true)String pageSize){
        try {
            ResponseMessage.success(collectionService.selectList(request,memberId));
        } catch (Exception e) {
            String msg = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/collect/select----e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
    }

}
